<template>
  <div class="container">
    <el-card style="margin: 10px">
      <!-- form表单 -->
      <el-form label-position="left" inline size="small">
        <el-row type="flex" justify="space-between">
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                placeholder="根据文章标题搜索"
                v-model="pageForm.keyword"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="状态">
              <el-select v-model="pageForm.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="hClean">清除</el-button>
              <el-button type="primary" @click="hInquiry">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="success" @click="hAdd" icon="el-icon-edit"
                >新增技巧</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- alert提示 -->
      <el-alert title="" type="info" show-icon>
        数据一共{{ total }}条
      </el-alert>
      <!-- table表格 -->
      <el-table style="margin-top: 15px" :data="articleList">
        <el-table-column
          label="序号"
          type="index"
          :index="pageAdd"
          sortable
        ></el-table-column>
        <el-table-column label="文章标题" prop="title" width="400">
          <template v-slot="scope">
            {{scope.row.title}}
            <el-button type="text" class="el-icon-film" style="color:#0000ff" v-if="scope.row.videoURL" @click="openVideo(scope.row.videoURL)"></el-button>

          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="180">
      <template v-slot="scope">
          <span>{{ dateFormatting(scope.row.createTime) }}</span>
      </template>
      </el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            已{{ typeofStatus(scope.row.state) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template v-slot="scope">
            <el-button type="text" @click="hPreview(scope.row)">预览</el-button>
            <el-button type="text" @click="hChangeState(scope.row)">{{
              scope.row.state ? "禁用" : "启用"
            }}</el-button>
            <el-button
              type="text"
              @click="hEdit(scope.row)"
              :disabled="!scope.row.state"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="hDel(scope.row)"
              :disabled="!scope.row.state"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!-- pag分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          layout="prev,pager,next,sizes,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageForm.pagesize"
          :total="total"
          :page="pageForm.page"
          :current-page.sync="pageForm.page"
        ></el-pagination>
      </el-row>
    </el-card>

   <!-- 视频弹框 -->
      <div class='mask' v-if='videoState == true'>
      </div>
       <div  class="videomasks" v-if="videoState == true">
        <div class="close" @click='masksCloseFun'>
          <i class="el-icon-close"></i>
        </div>
        <meta name=referrer content=no-referrer>
       <video class="videol" :src="videoUrl"
        muted="false" controls="controls" autoplay="autoplay" ></video>
        </div>
    <!-- 新增弹窗 -->
    <!-- 弹窗: 新增||编辑 -->
    <el-dialog
      width="50%"
      :visible.sync="showDialog"
      :title="isEdit ? '修改技巧' : '新增技巧'"
      v-if="showDialog"
    >
      <articles-add
      :isEdit="isEdit"
      :backFormData = " formData"
      @success = "hsuccess"
      @close="hCleanFormData"
      ref="formAddRef"/>
    </el-dialog>

    <!-- 预览文章弹窗 -->
    <el-dialog
      width="55%"
      :visible.sync="PreviewShowDialog"
      title="预览文章"
      v-if="PreviewShowDialog"
    >
      <articles-preview
      :articleContent="articleContent"
      />
    </el-dialog>

    <!-- 视频弹窗 -->

  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import { parseTime } from '@/utils/index.js'
// 导入子组件
import articlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'

// 导入枚举信息
import { status } from '@/api/hmmm/constants.js'
const sta = status.reduce((acc, cur) => {
  acc[cur.value] = cur.label
  return acc
}, {})
export default {
  components: {
    articlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      articleList: [],
      pageForm: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 5,
      showDialog: false,
      videoState: false,
      PreviewShowDialog: false,
      videoUrl: '',
      // 父传子-video
      articleContent: {},
      // 回填数据
      formData: {}
    }
  },
  created () {
    this.loadArt()
  },
  methods: {
    masksCloseFun () {
      this.videoState = false
    },
    // 格式化处理录入时间
    dateFormatting (date) {
      return parseTime(date)
    },
    // 调用子组件方法清空
    hCleanFormData () {
      // console.log(this.$refs.formAddRef.hClean)
      this.showDialog = false
      this.$refs.formAddRef.hClean()
    },
    // 更新成功
    hsuccess () {
      this.pageForm.page = 1
      // 刷新页面
      this.loadArt()
      // 关闭弹窗
      this.showDialog = false
    },
    // 删除弹窗提醒
    async hDel (row) {
      this.$confirm('此操作将永久删除该学科,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(row)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 做删除
    async doDel (row) {
      try {
        const res = await remove(row)
        // console.log(res)
        if (res.status === 200) {
          this.$message.success('删除成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }

      // 解决删除后当前页面数据为空bug
      if (this.articleList.length === 1) {
        let page = this.pageForm.page
        page--

        page = Math.max(1, page)
        this.pageForm.page = page
      }
      this.loadArt()
    },
    // 预览文章弹窗
    async hPreview (data) {
      console.log('Preview')
      // const res = await detail(data)
      // console.log('详情', res)
      this.PreviewShowDialog = true
      this.articleContent = data
    },
    // 视频播放
    openVideo (url) {
      this.videoState = true
      this.videoUrl = url
    },
    // 更改状态 启用||禁用
    async hChangeState (data) {
      try {
        const letState = data.state ? 0 : 1
        const res = await changeState({
          id: data.id,
          state: letState
        })
        if (res.status === 200) {
          this.$message.success('操作成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }
      // 更新
      this.loadArt()
    },

    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      // 回到第一页
      this.pageForm.page = 1
      this.pageForm.pagesize = val

      // 刷新
      this.loadArt(this.pageForm)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pageForm.page = val

      // 刷新
      this.loadArt(this.pageForm)
    },

    // 状态格式转换
    typeofStatus (status) {
      return sta[status]
    },

    // 新增弹窗
    hAdd () {
      this.showDialog = true
      this.isEdit = false
    },

    // 修改弹窗
    hEdit (val) {
      this.showDialog = true
      this.isEdit = true
      // 数据回填
      this.formData = {
        ...val
      }
    },

    // 获取文章列表
    async loadArt () {
      try {
        const res = await list(this.pageForm)
        console.log(res)
        this.articleList = res.data.items
        this.total = res.data.counts
      } catch (error) {}
    },

    // 清空表单
    hClean () {
      this.pageForm = {
        page: 1,
        pagesize: 10,
        title: ''
      }
    },

    // 点击查询
    hInquiry () {
      this.loadArt()
    },

    // 连续序号
    pageAdd (index) {
      return (this.pageForm.page - 1) * this.pageForm.pagesize + index + 1
    }
  }
}
</script>

<style scoped lang='scss'>
.mask{
   position:fixed ;
   top:0;
   left:0;
   bottom:0;
   right:0;
   z-index:1002;
   background-color: #000000;
   opacity: .6;
   }
   .close {
  position: fixed;
  left: 50%;
  top: 10%;
  transform: translate(-50%,-50%);
  font-size: 30px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: rgba(10, 12, 27, 0.116);
  z-index: 1002;
  padding-top: 15px;
  padding-left: 15px;
}

.videomasks{
max-width: 1200px;
width: 100%;
height: 100%;
position: fixed;
left: 50%;
top: 50%;
z-index: 1002;
transform: translate(-50%,-50%);
}

.videomasks .videol{
  position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
width: 800px;
height: 500px;
};

video:focus {
        outline: medium;
      }

</style>
